<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LIFT HTTP API Test Page</title>
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/bootstrap-icons.css') }}" rel="stylesheet">

    <style>
        .btn {
            height: 60px; /* Increase button height */
            font-size: 18px; /* Adjust font size for better readability */
        }
        .btn + .btn {
            margin-top: 20px; /* Increase vertical spacing between buttons */
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h1 class="mb-4">LIFT HTTP API TEST PAGE</h1>
        
        <div class="alert mb-4" id="statusAlert" style="display: none;">
            <div class="d-flex justify-content-between align-items-center">
                <span id="statusText">Checking connection status...</span>
                <span id="statusIndicator" class="badge">Unknown</span>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <button class="btn btn-primary w-100 mb-2" onclick="sendCommand('move_to_position_one')">
                    移动到一号位 <i class="bi bi-arrow-right ms-2"></i>
                </button>
                <button class="btn btn-primary w-100 mb-2" onclick="sendCommand('move_to_position_two')">
                    移动到二号位 <i class="bi bi-arrow-right ms-2"></i>
                </button>
                <button class="btn btn-primary w-100 mb-2" onclick="sendCommand('move_to_position_three')">
                    移动到三号位 <i class="bi bi-arrow-right ms-2"></i>
                </button>
                <button class="btn btn-success w-100 mb-2" onclick="sendCommand('move_up')">上升
                    <i class="bi bi-arrow-up ms-2"></i>
                </button>
                <button class="btn btn-danger w-100 mb-2" onclick="sendCommand('move_down')">下降
                    <i class="bi bi-arrow-down ms-2"></i>
                </button>
                <button class="btn btn-warning w-100 mb-2" onclick="sendCommand('reset')">复位

                    <i class="bi bi-arrow-counterclockwise ms-2"></i>
                </button>
                <button class="btn btn-info w-100 mb-2" onclick="sendCommand('stop_moving_up')">停止上升

                    <i class="bi bi-align-top ms-2"></i>
                </button>
                <button class="btn btn-info w-100 mb-2" onclick="sendCommand('stop_moving_down')">停止下降

                    <i class="bi bi-align-bottom ms-2"></i>
                </button>
            </div>
        </div>
    </div>
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <script src="{{ url_for('static', filename='js/jquery-3.7.1.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/bootstrap.bundle.min.js') }}"></script>
    <script>
        // Check connection status on load and every 5 seconds
        function checkStatus() {
            fetch('/api/lift/status')
                .then(response => response.json())
                .then(data => {
                    const alert = document.getElementById('statusAlert');
                    const text = document.getElementById('statusText');
                    const indicator = document.getElementById('statusIndicator');
                    
                    alert.style.display = 'block';
                    if (data.connected) {
                        alert.className = 'alert alert-success mb-4';
                        text.textContent = 'Lift is connected and ready';
                        indicator.textContent = 'Connected';
                        indicator.className = 'badge bg-success';
                    } else {
                        alert.className = 'alert alert-danger mb-4';
                        text.textContent = data.message || 'Lift is disconnected';
                        indicator.textContent = 'Disconnected';
                        indicator.className = 'badge bg-danger';
                    }
                })
                .catch(error => {
                    console.error('Status check failed:', error);
                });
        }

        // Initial status check
        checkStatus();
        // Periodic status check
        setInterval(checkStatus, 5000);

        function sendCommand(command) {
            const buttons = document.querySelectorAll('button');
            buttons.forEach(btn => btn.disabled = true);
            
            fetch(`/api/lift/${command}`, {
                method: 'POST'
            }).then(response => {
                if (response.ok) {
                    return response.json().then(data => {
                        showToast(`${command} command succeeded`, 'success');
                    });
                } else {
                    return response.json().then(data => {
                        showToast(`${command} failed: ${data.error || 'Unknown error'}`, 'danger');
                    });
                }
            }).catch(error => {
                showToast(`Network error: ${error.message}`, 'danger');
                console.error('Error:', error);
            }).finally(() => {
                buttons.forEach(btn => btn.disabled = false);
            });
        }

        function showToast(message, type) {
            const toast = document.createElement('div');
            toast.className = `toast align-items-center text-white bg-${type} border-0`;
            toast.setAttribute('role', 'alert');
            toast.setAttribute('aria-live', 'assertive');
            toast.setAttribute('aria-atomic', 'true');
            
            toast.innerHTML = `
                <div class="d-flex">
                    <div class="toast-body">
                        ${message}
                    </div>
                    <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
                </div>
            `;
            
            const toastContainer = document.getElementById('toastContainer');
            toastContainer.appendChild(toast);
            
            const bsToast = new bootstrap.Toast(toast);
            bsToast.show();
            
            // Auto-remove after hide
            toast.addEventListener('hidden.bs.toast', () => {
                toast.remove();
            });
        }
    </script>
        <!-- Toast container -->
        <div id="toastContainer" class="position-fixed bottom-0 end-0 p-3" style="z-index: 11"></div>
    </body>
</html>
